package com.dysoft.flesh.gl;

import com.dysoft.bones.AwtInputListener;
import com.dysoft.flesh.Renderer;
import com.sun.opengl.util.Animator;
import org.fenggui.Display;
import org.fenggui.binding.render.jogl.JOGLBinding;

import javax.media.opengl.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;

/**
 * @author Sean Micklethwaite
 *         Aug 14, 2010 8:32:36 PM
 */
public class GLWindow extends Frame implements WindowListener {
	final GLCanvas canvas;
	final Animator animator;
	final GLRenderer renderer;
	final Cursor hiddenCursor;

	final AwtInputListener input = new AwtInputListener();

	public GLWindow(Renderer renderer) throws Exception {
		super("GLWindow");

		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		setLayout(new BorderLayout());

		GLCapabilities caps = new GLCapabilities();
		caps.setDoubleBuffered(true);
		caps.setHardwareAccelerated(true);
		caps.setDepthBits(24);

		canvas = new GLCanvas(caps);
		canvas.setGL(new DebugGL(canvas.getGL()));

		addWindowListener(this);
		addKeyListener(input);
		addMouseListener(input);
		addMouseMotionListener(input);
		addMouseWheelListener(input);
		
		canvas.addKeyListener(input);
		canvas.addMouseListener(input);
		canvas.addMouseMotionListener(input);
		canvas.addMouseWheelListener(input);
		
		this.renderer = new GLRenderer(renderer, canvas);
		canvas.addGLEventListener(this.renderer);
		this.add(canvas);
		
		setSize(600, 600);
        setLocation(40, 40);

		setResizable(false);
        setVisible(true);
		
		this.animator = new Animator(canvas);
		animator.start();


		Toolkit toolkit = Toolkit.getDefaultToolkit();

		// get the smallest valid cursor size
		Dimension dim = toolkit.getBestCursorSize(1, 1);

		// create a new image of that size with an alpha channel
		BufferedImage cursorImg = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);

		// get a Graphics2D object to draw to the image
		Graphics2D g2d = cursorImg.createGraphics();

		// set the background 'color' with 0 alpha and clear the image
		g2d.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
		g2d.clearRect(0, 0, dim.width, dim.height);

		// dispose the Graphics2D object
		g2d.dispose();

		// now create your cursor using that transparent image
		hiddenCursor = toolkit.createCustomCursor(cursorImg, new Point(0,0), "hiddenCursor");

		//hideCursor();
	}

	public void hideCursor() {
		setCursor(hiddenCursor);
		input.captureMouse(true);
	}

	public void showCursor() {
		setCursor(Cursor.getDefaultCursor());
		input.captureMouse(false);
	}

	public void windowOpened(WindowEvent e) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void windowClosing(WindowEvent e) {
		this.setVisible(false);
		this.dispose();
		animator.stop();
	}

	public void windowClosed(WindowEvent e) {

	}

	public void windowIconified(WindowEvent e) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void windowDeiconified(WindowEvent e) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void windowActivated(WindowEvent e) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void windowDeactivated(WindowEvent e) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public GLRenderer getRenderer() {
		return renderer;
	}
}
